package DateUtil;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by areehy on 2016/8/12.
 */
public class FileUtil {
/**
 　　* 保存文件
 　　* @param toSaveString
 　　* @param filePath
 　　*/
public static void saveFile(String toSaveString, String filePath){
        try
        {
            File saveFile = new File(filePath);
            if (!saveFile.exists())
            {
                File dir = new File(saveFile.getParent());
                dir.mkdirs();
                saveFile.createNewFile();
            }

            FileOutputStream outStream = new FileOutputStream(saveFile);
            outStream.write(toSaveString.getBytes());
            outStream.close();
            } catch (FileNotFoundException e)
        {
            e.printStackTrace();
            } catch (IOException e)
        {
            e.printStackTrace();
            }
    }

   /**
     　　* 读取文件内容
     　　* @param filePath
     　　* @return 文件内容
   */
   public static String readFile(String filePath){
        String str = "";
        try
        {
            File readFile = new File(filePath);
            if(!readFile.exists())
            {
                return null;
                }
            FileInputStream inStream = new FileInputStream(readFile);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = inStream.read(buffer)) != -1)
            {
                stream.write(buffer, 0, length);
                }
            str = stream.toString();
            stream.close();
            inStream.close();
            return str;
            }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            return null;
            }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
            }
        }
}
